// pkg_wo/pages/perSet/perSet.js
import request from '../../../utils/http.js'
const app = getApp()
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    personId: 0,
    niCheng: '',
    xingMing:'',
    touXiangUrl: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
    IsLogin: '',
    avatarUrl: defaultAvatarUrl,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    this.setData({
      personId: options.personId,
    })


    // console.log(options)

    this.gettouxiang()
  },



  onChooseAvatar(e) {
    const {
      avatarUrl
    } = e.detail
    this.setData({
      avatarUrl: avatarUrl
    })
    this.uploadToQiniu(avatarUrl)
  },





  //获取头像，昵称
  gettouxiang: function () {
    var that = this

      request.get('person/getOneNiCheng', null, res => {
 
           console.log("头像昵称:" + JSON.stringify(res))

        that.setData({
          niCheng: res.data.niCheng == 'undefined' ? '微信用户' : res.data.niCheng,
          touXiangUrl: res.data.touXiangUrl == 'undefined' ? '' : res.data.touXiangUrl,
          xingMing:res.data.xingMing,
        })
      }, err => {
          wx.showToast({
            title: '查询失败', // 提示的内容
            icon: "none", // 图标，默认success
            duration: 1000, // 提示的延迟时间，默认1500
            mask: false, // 是否显示透明蒙层，防止触摸穿透
          })
        })
  },


  //修改名称
  bindNameInput: function (e) {
    console.log(e.detail.value)
    this.setData({
      niCheng: e.detail.value
    })
  },

    //修改名称
    bindxmInput: function (e) {
      console.log(e.detail.value)
      this.setData({
        xingMing: e.detail.value
      })
    },

  //修改头像
  bindKaotiImgFront: function (e) {
    var that = this
    wx.showActionSheet({
      itemList: ['拍照', '从相册选择'],
      success: function (res) {
        //拍照
        if (res.tapIndex == 0) {
          that.openCamera()
        } else if (res.tapIndex == 1) {
          //从相册选择
          that.openPhoto()
        }
      }
    })
  },

  /**
   * 打开照相机
   */
  openCamera: function () {
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ["compressed"],
      sourceType: ["camera"],
      success: function (res) {
        var filePath = res.tempFilePaths[0]
        var sizeImg = res.tempFiles[0].size

        if (sizeImg > 512000) { //logo大于500KB
          wx.showToast({
            title: '个人头像图片不能大于500K', // 提示的内容
            icon: "none", // 图标，默认success
            duration: 1500, // 提示的延迟时间，默认1500
            mask: false, // 是否显示透明蒙层，防止触摸穿透
          })

        } else {
          that.uploadToQiniu(filePath)
        }
        // console.log('sizeImg:'+sizeImg)

      },
    })
  },

  /**
   * 从相册选择
   */
  openPhoto: function () {
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ["compressed"],
      sourceType: ["album"],
      success: function (res) {
        var filePath = res.tempFilePaths[0]
        var sizeImg = res.tempFiles[0].size

        if (sizeImg > 512000) { //logo大于500KB
          wx.showToast({
            title: '个人头像图片不能大于500K', // 提示的内容
            icon: "none", // 图标，默认success
            duration: 1500, // 提示的延迟时间，默认1500
            mask: false, // 是否显示透明蒙层，防止触摸穿透
          })

        } else {
          that.uploadToQiniu(filePath)
        }
      },
    })
  },



  /**
   * 上传图片到七牛
   */
  uploadToQiniu: function (avatarUrl) {
    wx.showLoading({
      title: '上传中...',
      mask: true
    })
    var that = this
    console.log(avatarUrl)
    wx.request({
      url: app.globalData.base_url + 'resource/getQiNiuToken',
      success: function (res) {

        // console.log("res.data.uptoken:" + res.data.uptoken),
        // console.log("res.data.key:" + res.data.key)
        wx.uploadFile({
          url: 'https://up-z1.qbox.me', //七牛云存储华东地区
          filePath: avatarUrl,
          name: 'file',
          formData: {
            token: res.data.uptoken,
            key: res.data.key
          },
          success: function (res) {
            //  console.log("imageUrl:res.data:" + res.data)
            var dataObject = JSON.parse(res.data);
            var imageUrl = app.globalData.base_qiniu_url + dataObject.key;
            // console.log("imageUrl:" + imageUrl)
            that.setData({
              touXiangUrl: imageUrl
            })



          }



        })
      },
      complete: function (res) {
        wx.hideLoading();
      }
    })
  },





  //更新头像信息
  updateTouxiang: function () {
    var that = this

    let pages = getCurrentPages(); //定义上页面
    let currPage = null; //当前页面
    let prevPage = null; //上一个页面
    if (pages.length >= 2) {
      currPage = pages[pages.length - 1]; //当前页面   
      prevPage = pages[pages.length - 2]; //上一个页面        
      if (prevPage) {
        prevPage.setData({
          isUpdataNichengFlag: true
        });
      }
    }

    let data = {
      personId: that.data.personId,
      niCheng: that.data.niCheng,
      touXiangUrl: that.data.touXiangUrl,
      xingMing:that.data.xingMing
    }
    request.get('person/updateOneNiCheng', data, res => {

      wx.setStorageSync(app.globalData.NiCheng, that.data.niCheng)
      wx.setStorageSync(app.globalData.TouXiang, that.data.touXiangUrl)

      wx.navigateBack({
        delta: 1
      })
    }, err => {

    }, complete => {})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})